<?php
namespace app\app\controller;

use think\Controller;
class Home extends Controller {
    private $baseinfo;
    private $product;
    private $message;
    
    public function _initialize() {
        $this->baseinfo = new Baseinfo();
        $this->product  = new Product();
        $this->message  = new Message();
    }

    /*
     * app--首页--信息获取
     * 1.获取基本信息，头像、店名
     * 2.获取系统消息
     * 3.获取所有商品信息
     * @return 
     */
    public function index() {
        if(request()->isPost()) {
            $data = input('post.');
            $username = $data['post_username'];
            // 基本信息
            $dataArr['baseinfo'] = $this->baseinfo->index($username);
            // 商品信息
            $dataArr['products'] = $this->product->all();
            // 消息已阅读状态
            $dataArr['messageState'] = $this->message->messageState($username);
            return json_encode($dataArr, 320);
        }
    }
    
    /*
     * app获取单个商品信处
     * @return 
     */
    public function product() {
        if(request()->isPost()) {
            $data = input('post.');
            $id = $data['id'];
            $ret = $this->product->single($id);
            return json_encode($ret, 320);
        }
    }
    
    /*
     * app单个商品购买生成的<报表>中的订单信息
     * @return 
     */
    public function buy() {
        if(request()->isPost()) {
            $data     = input('post.');
            $id       = $data['id'];
            $username = $data['post_username'];
            $orderId  = $data['order_id'];
            $ret = $this->product->buy($id, $username, $orderId);
            return json_encode($ret, 320);
        } else {
            // 非法请求
            return json_encode(4501);
        }
    }
    
    /*
     * app通知信息内容获取
     * @return 
     */
    public function message() {
        if(request()->isPost()) {
            //获取相关的数据
            $data = input('post.');
            $username = $data['post_username'];
            $ret = $this->message->message($username);
            return json_encode($ret, 320);
        }
    }
    
    /*
     * app通知信息已阅读处理
     * @return 
     */
    public function megRead() {
        if(request()->isPost()) {
            //获取相关的数据
            $data = input('post.');
            $id = $data['get_id'];
            $ret = $this->message->megRead($id);
            return json_encode($ret, 320);
        }
    }
    
    /*
     * app通知信息已删除处理
     * @return 
     */
    public function megDelete() {
        if(request()->isPost()) {
            //获取相关的数据
            $data = input('post.');
            $id = $data['get_id'];
            $ret = $this->message->megDelete($id);
            return json_encode($ret, 320);
        }
    }
}